/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/***********************************************************************
 *
 * FILENAME:
 * myStripChart.h
 *
 * PURPOSE:
 *   Provide a moving strip chart for the user.
 *
 * CREATED: 
 *   
 *
 * LAST MODIFIED:
 * $Author: dixonc $
 * $Date: 2005/07/13 20:34:40 $
 * $Revision: 1.7 $
 *
 ***********************************************************************/

#ifndef _GTKSTRIPCHART_H
#define _GTKSTRIPCHART_H

#include <inttypes.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gconf/gconf-client.h>
#include <gtk/gtk.h>
#include <libgnomeui/libgnomeui.h>

#define MAX_NUM_DATA_SETS 10
#define NUM_GRAPH_POINTS  200
#define GTKSTRIPCHART_MIN_UPDATE 0.05f
#define GTKSTRIPCHART_MAX_UPDATE 100.0f
#define GTKSTRIPCHART_MIN_UPDATE_MSEC 50

enum VAR_TYPES
{
	NO_DEFINED_T = 0,
	CHAR_T,
	INT8_T,
	INT16_T,
	INT32_T,
	INT64_T,
	UINT8_T,
	UINT16_T,
	UINT32_T,
	UINT64_T,
	FLOAT_T,
	DOUBLE_T
};

enum AXIS_BOUND_MODE
{
	AUTO_SCALE_VISIBLE,   // auto scale the vertical axis with data shown in chart
	AUTO_SCALE_ALL,       // auto scale the vertical axis with data over all time
	NO_AUTO_SCALE
};

typedef struct _GtkStripChart GtkStripChart;
typedef struct _GtkStripChartConfig GtkStripChartConfig;

/* User interface functions */
GtkStripChart * gtkstripchart_new ( gint numDataSets, const gchar *names[], void * setPtr[], int setType[], gchar *name = NULL);           
void gtkstripchart_draw (GtkStripChart *g); 		/* Force a drawing update */
void gtkstripchart_start (GtkStripChart *g); 	/* Start graph. */
void gtkstripchart_stop (GtkStripChart *g); 		/* Stop graph. */

void gtkstripchart_add_point(GtkStripChart *chart, unsigned int dataSet, float val);
void gtkstripchart_add_all_points(GtkStripChart *chart, unsigned int numSets, float *val);

int gtkstripchart_num_charts();

void gtkstripchart_set_legend_table_draw (GtkStripChart *chart, int onOff);
void gtkstripchart_set_legend_draw (GtkStripChart *chart, int onOff);

void gtkstripchart_destroy (GtkStripChart *g);
void gtkstripchart_destroy_cb (GtkWidget *widget, gpointer data_ptr);
void gtkstripchart_destroyall();

/* Callbacks */
void cb_preferences_activate (GtkMenuItem *menuitem, gpointer user_data);
void cb_color_changed (GnomeColorPicker *cp, guint r, guint g, guint b, guint a, gpointer data);
gint cb_timeout (gpointer data);
gboolean cb_app_delete (GtkWidget *window, GdkEventAny *event, gpointer data);
void draw_legend_toggled(GtkToggleButton *togglebutton, gpointer data);
void draw_legend_table_toggled (GtkToggleButton *togglebutton, gpointer data);
void axis_mode_toggled(GtkToggleButton *togglebutton, gpointer data);

/* Preferences pop-up */
void gtkstripchart_create_preferences_dialog (GtkStripChart *chart);
void prefs_dialog_button_pressed (GtkDialog *dialog, gint id, gpointer data);
void timeouts_changed_cb (GConfClient *client, guint id, GConfEntry *entry, gpointer data);
void color_changed_cb (GConfClient *client, guint id, GConfEntry *entry, gpointer data);

gboolean gtkstripchart_button_press(GtkWidget *widget, GdkEventButton *event, gpointer user_data);

void update_graph_update_interval (GtkWidget *widget, gpointer data);
void bg_color_changed (GnomeColorPicker *cp, guint r, guint g, guint b, guint a, gpointer data);
void frame_color_changed (GnomeColorPicker *cp, guint r, guint g, guint b, guint a, gpointer data);
void timeouts_changed_cb (GConfClient *client, guint id, GConfEntry *entry, gpointer data);
void color_changed_cb (GConfClient *client, guint id, GConfEntry *entry, gpointer data);
void legend_changed_cb (GConfClient *client, guint id, GConfEntry *entry, gpointer data);
void legend_table_changed_cb (GConfClient *client, guint id, GConfEntry *entry, gpointer data);

/*
void set_chart_type(GtkStripChart *g, uint8_t type);
void set_chart_id(GtkStripChart *g, uint16_t id);
void set_chart_num(GtkStripChart *g, uint8_t num);
char *get_gconf_str(GtkStripChart *g);
void set_gconf_str(GtkStripChart *g, char *str);
*/

int gtkstripchart_update (GtkStripChart *g);
void gconf_getChartConfig(GtkStripChart *g, gchar *name = NULL);

// we use this to update the indiviual plot colors
typedef struct _GraphColor {
	GtkStripChart *g;
	int            ind;
} GraphColor;

struct _GtkStripChartConfig
{
	// Main window
	gint            width;
	gint            height;
	int             draw_legend;
	int             draw_legend_table;

	// graphs
	GdkColor        bg_color;
	GdkColor        frame_color;
	GdkColor        data_color[MAX_NUM_DATA_SETS];

	GraphColor      graphColor[MAX_NUM_DATA_SETS];

	// gtk
	gint            update_interval;

	// gconf
	char *          gconf_str;

	AXIS_BOUND_MODE axisMode;
};

struct _GtkStripChart 
{
	GtkStripChartConfig config;

	gint       index;			/* graph type index (index number) */

	gboolean   draw;
	gint       draw_width;
	gint       draw_height;
	int        draw_legend;		/* legend in graph display */
	int        draw_legend_table;	/* legend in table below display */

	gint       colors_allocated;
	GdkColor  *colors;
	GdkGC	    *gc;

	guint      allocated;		/* memory has been alloated  */
	guint      num_points;		/* number of data piont to plot */
	guint      num_sets;			/* number of data sets for this plot */
	gfloat   **data, **odata;	/* allocated memory for graph */
	guint      data_size;		/* allocated memory size of graph */
	guint     *pos;
	gfloat     maxVal;
	gfloat     minVal;

	GtkWidget *parent;
	GtkWidget *main_widget;
	GtkWidget *disp;
	GdkFont   *font;
	GdkPixmap *pixmap;
	GtkWidget *legendTable;
	GtkWidget *label[MAX_NUM_DATA_SETS];		/* label of data sets */
	char       dataNames[MAX_NUM_DATA_SETS][64];		/* label of data sets */

	int        paramType[MAX_NUM_DATA_SETS];
	void *     paramPtr[MAX_NUM_DATA_SETS];

	int        timer_index;		/* update timer */
	gint       update_interval;
};


#endif // _GTKSTRIPCHART_H
